<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function reverseWord(s) {
            // 反转字符串
            function reverseStr(s, l, r) {
                while (l < r) {
                    [s[l], s[r]] = [s[r], s[l]]
                    l++
                    r--
                }
                return s
            }
            // // 字符串转数组
            // // 移除多余空格
            const str = /\s+/g
            let strArr = Array.from(s.replace(str, ' ').trim())
            strArr = reverseStr(strArr, 0, strArr.length - 1)
            let start = 0
            for (let i = 0; i <= strArr.length; i++) {
                if (strArr[i] === ' ' || i === strArr.length) {
                    strArr =  reverseStr(strArr, start, i - 1)
                    start = i + 1
                    console.log(strArr)
                }
            }
            return Array.from(strArr).join('')
        }

        // 删除多余空格
        function removeExtraSpaces(strArr) {
            // let slowIndex = 0;
            // let fastIndex = 0;

            // while (fastIndex < strArr.length) {
            //     // 移除开始位置和重复的空格
            //     if (strArr[fastIndex] === ' ' && (fastIndex === 0 || strArr[fastIndex - 1] === ' ')) {
            //         fastIndex++;
            //     } else {
            //         strArr[slowIndex++] = strArr[fastIndex++];
            //     }
            // }

            // // 移除末尾空格
            // strArr.length = strArr[slowIndex - 1] === ' ' ? slowIndex - 1 : slowIndex;
        }
        console.log(reverseWord('   hello   who  is your daddy    '))
    </script>
</body>

</html>